from rest_framework.pagination import PageNumberPagination
from collections import OrderedDict
from response import APIResponse
from status import *


class MyPageNumberPagination(PageNumberPagination):
    # 重写4个类属性即可
    page_size = 5  # 每页显示两条
    page_query_param = 'page'  # 查询条件  http://127.0.0.1:8000/books/?page=2
    page_size_query_param = 'size'  # http://127.0.0.1:8000/books/?page=2&size=4 获取第二页数据，返回4条
    max_page_size = 20  # http://127.0.0.1:8000/books/?page=2&size=400 获取第二页数据，最多返回5条

    def get_paginated_response(self, data):
        print(data)
        return APIResponse(code=SUCCESS, msg="获取成功", data=OrderedDict([
            ('count', self.page.paginator.count),
            ('next', self.get_next_link()),
            ('previous', self.get_previous_link()),
            ('results', data)
        ]))
